package it.appcatalogobase;

import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.app.ActionBar.TabListener;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class Main2Activity extends FragmentActivity implements
TabListener {
	 private ViewPager viewPager;
	 private TabsPagerAdapter mAdapter;
	 private android.app.ActionBar actionBar;
	    // Tab titles
	 private String[] tabs = { "HOME", "CATALOGO", "CHI SIAMO","DOVE SIAMO" };
	

	 @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main2);
		 SharedPreferences prefs = getSharedPreferences("SyncPref", Context.MODE_PRIVATE);

		if(!prefs.getBoolean("FirstSync", true))
			Toast.makeText(this, "E' necessario sincronizzare almeno una volta l'app. Vai nella sezione catalogo", Toast.LENGTH_LONG);
//		if (savedInstanceState == null) {
//			getSupportFragmentManager().beginTransaction()
//					.add(R.id.container, new PlaceholderFragment()).commit();
//		}
		
		// Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);      
        
        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener( this));
         }
        
    	/**
    	 * on swiping the viewpager make respective tab selected
    	 * */
    	viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    	 
    	    @Override
    	    public void onPageSelected(int position) {
    	        // on changing the page
    	        // make respected tab selected
    	        actionBar.setSelectedNavigationItem(position);
    	    }
    	 
    	    @Override
    	    public void onPageScrolled(int arg0, float arg1, int arg2) {
    	    }
    	 
    	    @Override
    	    public void onPageScrollStateChanged(int arg0) {
    	    }
    	});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main2, menu);
		return true;
	}

//	@Override
//	public boolean onOptionsItemSelected(MenuItem item) {
//		// Handle action bar item clicks here. The action bar will
//		// automatically handle clicks on the Home/Up button, so long
//		// as you specify a parent activity in AndroidManifest.xml.
//		int id = item.getItemId();
//		if (id == R.id.action_settings) {
//			return true;
//		}
//		return super.onOptionsItemSelected(item);
//	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
//	public static class PlaceholderFragment extends Fragment {
//
//		public PlaceholderFragment() {
//		}
//
//		@Override
//		public View onCreateView(LayoutInflater inflater, ViewGroup container,
//				Bundle savedInstanceState) {
//			View rootView = inflater.inflate(R.layout.fragment_main2,
//					container, false);
//			return rootView;
//		}
//	}


	@Override
	public void onTabReselected(android.app.ActionBar.Tab arg0,
			android.app.FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTabSelected(android.app.ActionBar.Tab arg0,
			android.app.FragmentTransaction arg1) {
//		Fragment fragment = null;
//		if(arg0.getPosition()==1)
//			fragment  = new HomeFragment();
//		else if(arg0.getPosition()==2)
//			fragment  = new ChiSiamoFragment();
//		else if(arg0.getPosition()==3)
//			fragment  = new CatalogoFragment();
		viewPager.setCurrentItem(arg0.getPosition());
		
	}

	@Override
	public void onTabUnselected(android.app.ActionBar.Tab arg0,
			android.app.FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		 
		
	}
	
	
	 public void listCategorie(View v){
			Intent i = new Intent(Main2Activity.this, CategoryListActivity.class);
			startActivity(i);
		}
		
		public void sync(View v){
			MyAsyncTask task = new MyAsyncTask("www.tempogara.it/arcobaleno/temp/catalogo.php?data=categorie", this);
			task.execute();
		}
		
		public void listAllProducts(View v){
			Intent i = new Intent(Main2Activity.this, ListProductsActivity.class);
			i.putExtra("type", "all");
			startActivity(i);
		}
	


}
